package bbs.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;


import com.whu.administrator.ws.R;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import bbs.model.CommitBack;
import bbs.net.GetCommitBack;
import utils.TimeUtil;

/**
 * Created by nimi on 2015/11/20.
 */
public class CommitBackAdapter extends BaseAdapter {
    private static List<GetCommitBack.BoardFeedback> commitBacks;
    private Context mContext;
    long days;
    long hours;
    public CommitBackAdapter(Context context, List<GetCommitBack.BoardFeedback> commitBacks) {
        this.mContext=context;
        this.commitBacks= commitBacks;
    }
    //返回已获得数据源总数量
    public int getCount() {
        // TODO Auto-generated method stub
        if (commitBacks != null)
            return commitBacks.size();
        else
            return 0;
    }

    @Override
    //告诉适配器取得目前容器中的数据对象
    public GetCommitBack.BoardFeedback getItem(int position) {
        // TODO Auto-generated method stub
        return commitBacks==null?null:commitBacks.get(position);

    }
    @Override
    //告诉适配器取得目前容器中的数据ID
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    //取得当前欲显示的图像View
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        LayoutInflater _LayoutInflater = LayoutInflater.from(mContext);
        convertView = _LayoutInflater.inflate(R.layout.bbs_reply_list, null);
        if (convertView != null) {
            TextView _TextView1 = (TextView) convertView.findViewById(R.id.postContent);
            _TextView1.setText(commitBacks.get(position).getFeedback_Content());
            TextView _TextView2=(TextView)convertView.findViewById(R.id.postTime);
            String time=commitBacks.get(position).getFeedback_Time();
            String timeNow= TimeUtil.currentTime();
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try
            {
                Date d1 = df.parse(time);
                Date d2 = df.parse(timeNow);
                long diff = d2.getTime() - d1.getTime();
                days = diff / (1000 * 60 * 60 * 24);
                hours=diff/(1000 * 60 * 60);

            }
            catch (Exception e)
            {

            }
            if(days>=1)
                _TextView2.setText((int)days+"天前");
            else{
                _TextView2.setText((int)hours+"小时前");
            }
            TextView _TextView3=(TextView)convertView.findViewById(R.id.postName);

            _TextView3.setText(commitBacks.get(position).getUser_RealName()+":");

        }
        return convertView;
    }
}
